import { BreakpointManager } from '@hadss/web_adaptive_layout_ui';

export type Breakpoints<T> = {
  xs?: T;
  sm?: T;
  md?: T;
  lg?: T;
  xl?: T;
  xxl?: T;
};
export type Breakpoint = 'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'xxl';
export type BreakpointValue = number | string | boolean;
const defaultBreakpoint: (keyof Breakpoints<BreakpointValue>)[] = ['xs', 'sm', 'md', 'lg', 'xl', 'xxl'];

export const getBreakpointsAttr = (value: unknown, breakpointManager: BreakpointManager) => {
  if (!breakpointManager || breakpointManager === null) {
    return value;
  }
  if (typeof value === 'object' && value !== null && defaultBreakpoint.some(item => item in value)) {
    return breakpointManager.useBreakpointValue(value);
  } else {
    return value;
  }
};

export const getbreakpointManager = (breakpointReference: string | null, rowBreakpoints: Breakpoints<number> | null, scopeId? : string): BreakpointManager => {
  if (!breakpointReference) {
    return new BreakpointManager();
  }

  if (breakpointReference === 'windowSize') {
    return new BreakpointManager(rowBreakpoints === null ? undefined : rowBreakpoints, undefined);
  } else {
    return new BreakpointManager(rowBreakpoints === null ? undefined : rowBreakpoints, scopeId);
  }
};